package com.nb.nike.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.CompositePageTransformer
import androidx.viewpager2.widget.MarginPageTransformer
import com.nb.nike.adapter.BrandAdapter
import com.nb.nike.adapter.PopularAdapter
import com.nb.nike.adapter.SliderAdapter
import com.nb.nike.databinding.ActivityMainBinding
import com.nb.nike.model.SliderModel
import com.nb.nike.viewModel.MainViewModel

class MainActivity : BaseActivity() {
    private val viewModel = MainViewModel()
    private lateinit var binding:ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initBanner()
        initBrand()
        initPopular()
        initBottomMenu()
    }

    private fun initBottomMenu() {
        binding.cartBtn.setOnClickListener{
            startActivity(Intent(this@MainActivity,CartActivity::class.java))
        }
    }

    private fun initBanner() {
        binding.progressBarBanner.visibility = View.VISIBLE

        viewModel.banners.observe(this,{items->
            banners(items)
            binding.progressBarBanner.visibility = View.GONE
        })

        viewModel.loadBanner()
    }

    private fun initPopular() {
        binding.progressBarPopular.visibility = View.VISIBLE

        viewModel.items.observe(this,{item->
            binding.viewPopular.layoutManager = GridLayoutManager(this@MainActivity,2)
            //banners(items)
            binding.viewPopular.adapter = PopularAdapter(item)
            binding.progressBarPopular.visibility = View.GONE
        })

        viewModel.loadPupolar()
    }

    private fun banners(images:List<SliderModel>){
        binding.viewpagerSlider.adapter = SliderAdapter(images,binding.viewpagerSlider)
        binding.viewpagerSlider.clipToPadding = false

        binding.viewpagerSlider.clipChildren = false
        binding.viewpagerSlider.offscreenPageLimit =3
        binding.viewpagerSlider.getChildAt(0).overScrollMode = RecyclerView.OVER_SCROLL_NEVER

        val compositePageTransformer = CompositePageTransformer().apply {
            addTransformer(MarginPageTransformer(40))
        }

        binding.viewpagerSlider.setPageTransformer(compositePageTransformer)

        if(images.size>1){
            binding.dotIndicator.visibility = View.VISIBLE
            binding.dotIndicator.attachTo(binding.viewpagerSlider)
        }
    }

    private fun initBrand(){
        binding.progressBarBanner.visibility = View.VISIBLE
        viewModel.brands.observe(this, Observer {item->
            binding.viewBrand.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
            binding.viewBrand.adapter = BrandAdapter(item)
            binding.progressBarBrand.visibility = View.GONE
        })

        viewModel.loadBrand()
    }
}
